#include "stream_resource_handler.h"

#include <stdbool.h>

#include "internal/cef_string_types.h"

#include "tools/file_utils.h"
#include "tools/log_util.h"

int CEF_CALLBACK process_request(
    struct _cef_resource_handler_t* self,
    struct _cef_request_t* request,
    struct _cef_callback_t* callback) {
    // 直接返回1表示请求已经处理
    callback->cont(callback); // 通知 CEF 继续处理
    return 1;
}

void CEF_CALLBACK get_response_headers(
    struct _cef_resource_handler_t* self,
    struct _cef_response_t* response,
    int64_t* response_length,
    cef_string_t* redirectUrl) {
    stream_resource_handler_t *handler = (stream_resource_handler_t *)self;

    // 设置http状态码和状态文本
    response->set_status(response, handler->status_code);
    response->set_status_text(response, &handler->status_text);

    // 设置MIME类型
    response->set_mime_type(response, &handler->mime_type);

    // 设置响应头
    size_t count = cef_string_multimap_size(handler->hearder_map);
    if (count > 0) {
        response->set_header_map(response, handler->hearder_map);
    }

    // 设置响应长度
    if (handler->stream) {
        *response_length = get_stream_length(handler->stream);
    } else {
        *response_length = 0;
    }
}

int CEF_CALLBACK skip(struct _cef_resource_handler_t* self,
                          int64_t bytes_to_skip,
                          int64_t* bytes_skipped,
                          struct _cef_resource_skip_callback_t* callback) {
    stream_resource_handler_t *handler = (stream_resource_handler_t *)self;

    // 检查流是否有效
    if (!handler->stream) {
        *bytes_skipped = -2;   // ERR_FAILED
        return 0;
    }

    // 跳过指定字节数
    *bytes_skipped = handler->stream->seek(handler->stream, bytes_to_skip, SEEK_CUR);

    // 返回是否成功
    if (*bytes_skipped == bytes_to_skip) {
        return 1;   // 成功跳过
    } else {
        *bytes_skipped = -2;   //ERR_FAILED
        return 0;
    }
}

 int CEF_CALLBACK read(struct _cef_resource_handler_t* self,
                          void* data_out,
                          int bytes_to_read,
                          int* bytes_read,
                          struct _cef_resource_read_callback_t* callback) {
    stream_resource_handler_t *handler = (stream_resource_handler_t *)self;

    // 检查流是否有效
    if (!handler->stream) {
        *bytes_read = -2;   // ERR_FAILED
        return 0;           // 失败
    }

    // 从流中读取数据
    *bytes_read = handler->stream->read(handler->stream, data_out, 1, bytes_to_read);

    // 返回数据读取情况
    if (*bytes_read > 0) {
        return 1;          // 成功读取
    } else {
        *bytes_read = 0;   // 数据读取完成
        return 0;          // 返回结束
    }
 }

 void CEF_CALLBACK cancel(struct _cef_resource_handler_t* self) {
    stream_resource_handler_t *handler = (stream_resource_handler_t *)self;

    // 清理资源
    if (handler->stream) {
        handler->stream->base.release(&handler->stream->base);
        handler->stream = NULL;
    }

    // 清理字符串
    cef_string_clear(&handler->mime_type);
    cef_string_clear(&handler->status_text);

    // 清理响应头
    if (cef_string_multimap_size(handler->hearder_map) > 0) {
        cef_string_multimap_clear(handler->hearder_map);
    }
    cef_string_multimap_free(handler->hearder_map);
 }

cef_resource_handler_t *create_stream_resource_handler(
    int status_code,
    const cef_string_t *status_text,
    const cef_string_t *mime_type,
    const cef_string_multimap_t hearder_map,
    cef_stream_reader_t *stream) {

    stream_resource_handler_t *handler = (stream_resource_handler_t *)calloc(1, sizeof(stream_resource_handler_t));
    // 初始化
    handler->handler.base.size = sizeof(cef_resource_handler_t);
    handler->handler.process_request = process_request;
    handler->handler.get_response_headers = get_response_headers;
    handler->handler.skip = skip;
    handler->handler.read = read;
    handler->handler.cancel = cancel;
    // 设置状态
    handler->status_code = status_code;
    cef_string_utf16_copy(status_text->str, status_text->length, &handler->status_text);
    cef_string_utf16_copy(mime_type->str, mime_type->length, &handler->mime_type);
    // 设置响应头
    handler->hearder_map = cef_string_multimap_alloc();
    if (hearder_map && cef_string_multimap_size(hearder_map) > 0) {
        cef_string_multimap_copy(hearder_map, handler->hearder_map);
    } 

    // 设置文件流
    handler->stream = stream;
    if (stream) {
        stream->base.add_ref(&stream->base);
    }
    LOG_DEBUG("exec end----------");
    return (cef_resource_handler_t *)handler;
}



    

